import logger from '../core/logger';
import { clientBase } from './core/clientBase';
import { followMange } from '../models/manga/followMange';
import { historyManga } from '../models/manga/historyManga';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class userClient extends clientBase {
  followList(order: number, page: number): Promise<followMange[] | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`page_num=${page}&page_size=15&order=${order}`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/ListFavorite', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: Array<followMange> = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  buy(page: number): Promise<historyManga[] | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`page_num=${page}&page_size=15`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/user.v1.User/GetAutoBuyComics', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: Array<historyManga> = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  history(page: number): Promise<historyManga[] | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`page_num=${page}&page_size=15`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/ListHistory', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: Array<historyManga> = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  follow(id: number): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`comic_ids=${id}`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/AddFavorite', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data);
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }

  unFollow(id: number): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`comic_ids=${id}`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/DeleteFavorite', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data);
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }

  delete(id: number): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`comic_ids=${id}`);
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/DeleteHistory', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data);
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }

  getSign(): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam();
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/activity.v1.Activity/GetClockInInfo', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data) && response.data.data?.status == 1;
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }

  signIn(): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam();
        axios.post<string, AxiosResponse<string>, any>('https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn', data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data);
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }
}
export default new userClient();